// pages/search/search.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myHttp: app.globalData.myHttp,
    inputValue:'',
    hospital:[],
    doctor:[],
    dapartment:[],
    tapIndex:'',
    searchHospital:'',
    searchDoctor:'',
    isFund:false,
    city:'',
  },

  //进行搜索
  search(e){
    var that = this
    if(!that.data.inputValue){
      wx.showToast({
        title: '请输入搜索的内容',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    wx.showLoading({title: '数据加载中', icon: 'loading'});
    type: 'wgs84',
      wx.getLocation({
        success: function (res) {
          console.log(res.latitude, res.longitude);
          latitude: res.latitude;
          // this.latitude = latitude
          longitude: res.longitude;
          // this.longitude = longitude
        },
        complete: () =>{
          wx.hideLoading()
        }
      })
      
    wx.request({
      url: this.data.myHttp + '/hospital/search',
      method: "GET",
      header: {
        'content-type': 'application/json',
      },
      data: {
        "keyword": this.data.inputValue, "type": 0, "status": 1, "Province": that.data.city
      },
      success: function (res) {
        console.log(res.data.obj.hospitalList.records)
        console.log(res.data.obj.doctorList.records)
        // console.log(this.inputValue)
        that.setData({
          searchHospital: res.data.obj.hospitalList.records,
          searchDoctor: res.data.obj.doctorList.records,
          isFund:true
        })
      }
      
      
      })
  },

  //获取input框中内容
  getInput(e){
    this.setData({
      inputValue:e.detail.value
    })
  },

  //图片加载失败使用同一图片代替
  binderrorimg:function(e){
    var errorImgIndex = e.target.dataset.errorimg //获取循环的小标
    var imgObject = "searchDoctor[" + errorImgIndex + "].imgUrl" //carlistData为数据源，对象数组
    var errorImg={}
    errorImg[imgObject] = "../../image/user.png" //我们构建一个对象
    this.setData(errorImg) //修改数据源对应的数据
  },


  //点击进入医院界面
  bindViewTapToHospital1: function (e) {
    console.log(e.currentTarget.dataset.hospital);
    var hospital = e.currentTarget.dataset.hospital
    this.setData({
      selectedHospital: e.currentTarget.dataset.hospital,
      tapIndex: e.currentTarget.dataset.index
    })
    console.log(this.data.selectedHospital)
    console.log(JSON.stringify(this.data.selectedHospital))
    if (hospital.secondHospitalList != null && hospital.secondHospitalList.length > 1)
      return 
    wx.navigateTo({
      url: '../hospitalIntroduction/hospitalIntroduction?selectedHospitalId=' + JSON.stringify(this.data.selectedHospital.id)
    })
  },

  // 跳转到医生详情
  selectyisheng: function (event) {

    this.setData({
      selectyisheng: event.currentTarget.dataset.doctor
    })
    console.log(this.data.selectyisheng)
    wx.navigateTo({
      url: '../doctordetails/doctordetails?selectyisheng=' + encodeURIComponent(JSON.stringify(this.data.selectyisheng)),
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let city = JSON.parse(options.city)
    this.setData({
      isFund:false,
      city:city
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})